package info.techgeek.labs.regex;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Hashtable;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;

public class DbConnection 
{
	
	public static Connection getConnection() 
	{
		Connection connection = null;
		Context context = null;
		DataSource dataSource = null;
		Hashtable table = new Hashtable();  
		table.put(Context.INITIAL_CONTEXT_FACTORY, "weblogic.jndi.WLInitialContextFactory");  
		table.put(Context.PROVIDER_URL,"t3://localhost:7001");
		
		try
		{
			context = new InitialContext(table);  
			dataSource  = (javax.sql.DataSource) context.lookup ("DeccanDBConn");  
			connection = dataSource.getConnection();
		}
		catch(NamingException ne)
		{
			ne.printStackTrace();
			
		}
		catch(SQLException se)
		{
			se.printStackTrace();
		}
		return connection;	
	}
	
	public static void closeDBObjects(Connection connection,Statement statement,ResultSet resultSet) {
		if (resultSet != null) 
		{
			try 
			{
				resultSet.close();
			} 
			catch (SQLException sqlException) 
			{
				
			}
		}
		if (statement != null) 
		{
			try 
			{
				statement.close();
			} 
			catch (SQLException sqlException)
			{
				
			}
		}
		if (connection != null) 
		{
			try 
			{
				connection.close();
			} 
			catch (SQLException sqlexception)
			{
				
			}
			
		}
	}
	
}
